package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.FlightMessage;
import cn.wolfcode.luowowo.article.domain.FlightOrder;
import cn.wolfcode.luowowo.article.mapper.FlightMessageMapper;
import cn.wolfcode.luowowo.article.mapper.FlightOrderMapper;
import cn.wolfcode.luowowo.article.service.IFlightOrderService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class FlightOrderServiceImpl implements IFlightOrderService{
    @Autowired
    private FlightOrderMapper flightOrderMapper;
    @Autowired
    private FlightMessageMapper flightMessageMapper;
    public void saveOrder(Long fid,Long uid) throws LogicException{
        if(fid==null){
            throw  new LogicException("非法操作");
        }
        FlightMessage flightMessage = flightMessageMapper.selectByPrimaryKey(fid);
        //判断当前机票是否被用户购买过
        //Integer length= flightOrderMapper.selectOrderByUserId(uid,flightMessage.getFlightNo());
        //System.out.println(length);
       /* if(null!=length||0!=length){
            //如果长度不为0或者不为null表示当前机票被用户购买了,提示用户你已经购买了
            throw  new LogicException("你已经购买了本次机票");
        }*/
        FlightOrder flightOrder = new FlightOrder();
        flightOrder.setUserId(uid);
        flightOrder.setArrPort(flightMessage.getArrPort());
        flightOrder.setArrTime(flightMessage.getArrTime());
        flightOrder.setDepPort(flightMessage.getDepPort());
        flightOrder.setDepTime(flightMessage.getDepTime());
        flightOrder.setDepartCity(flightMessage.getDepartCity());
        flightOrder.setDestCity(flightMessage.getDestCity());
        flightOrder.setName(flightMessage.getName());
        flightOrder.setPrice(flightMessage.getPrice());
        flightOrder.setFlightNo(flightMessage.getFlightNo());
        flightOrderMapper.insert(flightOrder);
    }

    @Override
    public List<FlightOrder> listById(Long id) {
        List<FlightOrder> flightOrders = flightOrderMapper.listById(id);
        return flightOrders;
    }
}
